//
//  FXChannel.m
//  WangYiTest
//
//  Created by 袁飞霞 on 16/5/11.
//  Copyright © 2016年 cn.sophietobeabetterone. All rights reserved.
//

#import "FXChannel.h"

@implementation FXChannel
+ (instancetype)channelWithDict:(NSDictionary *)dict {
    id obj = [[self alloc] init];
    
    [obj setValuesForKeysWithDictionary:dict];
    
    return obj;
}
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{}


- (void)setTid:(NSString *)tid {
    _tid = [tid copy];
    //    @"article/headline/T1348647853363/0-20.html"
    _URLString = [NSString stringWithFormat:@"article/headline/%@/0-20.html",tid];
}

/**
 *  返回所有的新闻频道数据
 */
+ (NSArray *)channels {
    NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"topic_news.json" ofType:nil]];
    
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
    // 获得第一个key
    NSString *rootKey = dict.keyEnumerator.nextObject;
    // 获得数组
    NSArray *array = dict[rootKey];
    
    NSMutableArray *channels = [NSMutableArray array];
    // 遍历数组
    [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [channels addObject:[FXChannel channelWithDict:obj]];
    }];
    
    // 排序
    return [channels sortedArrayUsingComparator:^NSComparisonResult(FXChannel  *obj1,   FXChannel  *obj2) {
        //        NSOrderedAscending = -1L, 升序
        //        NSOrderedSame, 相等
        //        NSOrderedDescending 降序
        return [obj1.tid compare:obj2.tid];
        
    }];
}
@end
